﻿@{
    ViewBag.Title = "WorkItemForm";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}
<script>
    var  organizeid = " @LeSoft.Application.Code.OperatorProvider.Provider.Current().CompanyId", DeviceLevel = [], ProjectType = [], DeviceTypeId = [], _TypeId = "";
    var _ExecRoleId="" ,   _ResposeDept ="";
    var keyValue = request('keyValue');
    var parentId = "0";
    $(function () {
        GetGrid();
        initControl();
        GetDeviceLevelList();
        GetTree();
       
        

    })
    function BaseClick(ss) {

        if (ss == 0) { }
        else if (ss == 1) {
            GetJson();
            $('#gridTable1').GridUnload();
            GetGrid2();
            $('#gridTable1').setGridWidth(747);
        }

    }
    function initControl() {
        $("#OrganizeId").ComboBoxTree({
            url: "../../BaseManage/Organize/GetTreeJsonById",
            description: "==选择所属机构==",
            height: "250px",
            click: function (data) {
                organizeid = data.id;
            }
        });
        $("#OrganizeId").ComboBoxSetValue(organizeid);
         
        //加载岗位
        $("#ExecRoleId").ComboBox({
            url: "../../BaseManage/Post/GetListJson?organizeId=" + organizeid,
            id: "RoleId",
            text: "FullName",
            description: "==请选择==",
            allowSearch: true
        });
        if (!!keyValue) {
            
            $.SetForm({
                url: "../../PropertyManage/CS_DeviceMaintance/GetWorkItemEntity",
                param: { keyValue: keyValue },
                success: function (data) {
                    $("#WIform").SetWebControls(data.data1);
                    _TypeId = data.data1.Type;
                    organizeid = data.data1.OrganizeId;
                    $("#TypeText").val(data.data5); 
                    $gridTable[0].addJSONData(JSON.parse(data.data1.ContentDetail));                   
                    str1 = data.data2.split(',');
                    str2 = data.data3.split(',');
                    str3 = data.data4.split(',');
                    obj = document.getElementsByName("checkLevel");
                    obj1 = document.getElementsByName("checkType");
                    for (var i = 0; i < str1.length; i++) {
                        DeviceLevel.push(str1[i]);
                    }
                    for (var i = 0; i < str2.length; i++) {
                        ProjectType.push(str2[i]);
                    }
                    for (var i = 0; i < str3.length; i++) {
                        DeviceTypeId.push(str3[i]);
                    }
                    GetGrid2();
                }
            });
        }
        var item = {
            url: "../../PropertyManage/CS_DeviceMaintance/GetCheckBoxTreeJson?DeviceTypeId=" + DeviceTypeId,
            height: "200px",
            showcheck: true,
            oncheckboxclick: function (item) {
                var keyValue = item.id;
                if (item.checkstate == 0) {
                    DeviceTypeId.push(keyValue);
                }
                else {
                    DeviceTypeId = $.grep(DeviceTypeId, function (value) {
                        return value != keyValue;
                    });
                }
                GetJson();
                $('#gridTable1').GridUnload();
                GetGrid2();
            }
        };
        $("#DeviceType").treeview(item);
    }
    function GetTree() {
        var item = {
            height: $(window).height() - 55.5,
            url: "../../PropertyManage/CS_DeviceMaintance/GetWorkTypeTreeJson",
            onnodeclick: function (item) {
                _TypeId = item.id;
                _ExecRoleId = item.ExecRoleId;
                _ResposeDept = item.ResposeDept;
                $("#TypeText").val(item.text);
                $('#btn_Search').trigger("click");
            }
        };
        //初始化
        $("#itemTree1").treeview(item);

    }
    function GetGrid(data) {
        $gridTable = $("#gridTable");
        $gridTable.jqGrid({
            datatype: "json",
            height: 180,
            autowidth: true,
            unwritten: false,
            colModel: [
                { label: "主键", name: "Id", hidden: true },
                {
                    label: "主键1", name: "Key", hidden: true,
                    formatter: function (cellvalue, options, rowObject)
                    {
                        return rowObject.Id;
                    }
                },
                { label: "序号", name: "Sort", index: "Sort", width: 100, align: "center", sortable: false },
                { label: "要点", name: "Title", index: "Title", width: 100, align: "center", sortable: false },
                { label: "说明", name: "Memo", index: "Memo", width: 100, align: "center", sortable: false }
            ],
            viewrecords: true,
            treeGrid: true,
            rowNum: "all",
            caption: "工作内容",
            shrinkToFit: false,
            hidegrid: false,
            rownumbers: false,

        });
       

    }
    function GetGrid2() {
        var queryJson = {
            Type: DeviceTypeId,
            Level: DeviceLevel,
            Project: ProjectType
        };
        $gridTable = $("#gridTable1");
        $gridTable.jqGrid({
            url: "../../PropertyManage/CS_DeviceMaintance/GetDeviceList",
            postData: { queryJson: JSON.stringify(queryJson) },
            datatype: "json",
            height: 190,
            autowidth: true,
            unwritten: false,
            colModel: [
                { label: "Id", name: "Id", hidden: true },
                { label: "设备名称", name: "Name", index: "Name", width: 100, align: "center", sortable: false },
                { label: "设备编号", name: "Code", index: "Code", width: 100, align: "center", sortable: false },
                { label: "设备类型", name: "Type", index: "Type", align: "center", sortable: false },
                { label: "设备等级", name: "Level", index: "Level", align: "center", sortable: false }
            ],
            viewrecords: true,
            rowNum: 10,
            rowList: [10, 30, 50, 100],
            pager: "#gridPager",
            sortname: 'Date',
            sortorder: 'desc',
            rownumbers: true,
            gridview: true,



        });
    }
    function GetDeviceLevelList() {
        

        $.ajax({
            url: "../../PropertyManage/CS_DeviceMaintance/GetDeviceLeveCheckBox?keyValue=" + DeviceLevel,
            type: "post",
            dataType: "text",
            success: function (data) {
                $("#Level").html(data);
            }
        });
        $.ajax({
            url: "../../PropertyManage/CS_DeviceMaintance/GetProjectTypeCheckBox?keyValue=" + ProjectType,
            type: "post",
            dataType: "text",
            success: function (data) {
                $("#ProjectType").html(data);
            }
        });
    }
    function GetDiviceList()
    {
        GetJson();
        $('#gridTable1').GridUnload();
        GetGrid2();
    }
    function GetJson() {
        obj = document.getElementsByName("checkLevel");
        obj1 = document.getElementsByName("checkType");
        DeviceLevel = [];
        ProjectType = [];
        
        for (k in obj) {
            if (obj[k].checked)
                DeviceLevel.push(obj[k].value);
        }
        for (j in obj1) {
            if (obj1[j].checked)
                ProjectType.push(obj1[j].value);
        }
    }
    function btn_add() {
        dialogOpen({
            id: "WorkContentForm",
            title: '添加内容',
            url: '/PropertyManage/CS_DeviceMaintance/ContentForm',
            width: "400px",
            height: "350px",
            callBack: function (iframeId) {
                top.frames["WorkContentForm"].AcceptClick();
            }
        })
    }
    function btn_edit() {
        var keyValue = $("#gridTable").getGridParam('selrow');
        if (checkedRow(keyValue)) {
            dialogOpen({
                id: "WorkContentForm",
                title: '添加内容',
                url: '/PropertyManage/CS_DeviceMaintance/ContentForm?keyValue=' + keyValue,
                width: "400px",
                height: "350px",
                callBack: function (iframeId) {
                    top.frames["WorkContentForm"].AcceptClick();
                }
            })
        }
    }
    function btn_delete() {
        var keyValue = $("#gridTable").getGridParam('selrow');
        if (keyValue) {
            dialogConfirm('注：您确定要移除吗？该操作将无法恢复？', function (r) {
                if (r) {
                    $("#gridTable").delRowData(keyValue);
                    dialogMsg("移除成功。", 1);
                }
            });

        } else {
            dialogMsg('请选择需要移除的数据！', 0);
        }
    }
    function GetRData() {
        var ContentDetail = [];
        var $gridTable = $("#gridTable");
        var count = $gridTable.getGridParam("records"); 
        for (var i = 1; i <= count; i++) { 
            var query = {
                Id: $gridTable.getCell(i, "Key"),
                Sort: $gridTable.getCell(i, "Sort"),
                Title: $gridTable.getCell(i, "Title"),
                Memo: $gridTable.getCell(i, "Memo")
            };
            ContentDetail.push(query);
        }
        return ContentDetail;
    }

    function AcceptClick() {
        
        if (!$('#WIform').Validform()) {
            return false;
        }
        GetJson();
        var DeviceId = [];
        var obj = $("#gridTable1").jqGrid("getRowData");
        //if (obj.length <= 0) {
        //    dialogMsg('请至少关联一台设备！', 0);
        //    return false;
        //} 
        var postData = $("#WIform").GetWebControls(keyValue);
        postData["ResposeDept"] = _ResposeDept;
        postData["Type"] = _TypeId;
        postData["ContentDetail"] = JSON.stringify(GetRData());
        if (postData["ExecRoleId"].length < 1)
            postData["ExecRoleId"] = _ExecRoleId;
        postData["RoleType"] = 2;
        $.SaveForm({
            url: " ../../PropertyManage/CS_DeviceMaintance/SaveWorkItemForm?keyValue=" + keyValue + "&DeviceLevel=" + DeviceLevel + "&ProjectType=" + ProjectType + "&DeviceTypeId=" + DeviceTypeId,
            param: postData,
            loading: "正在保存数据...",
            success: function () {
                $.currentIframe().$("#gridTable").resetSelection();
                $.currentIframe().$("#gridTable").trigger("reloadGrid");
                dialogClose();
            }
        })
    }
</script>

<div>
    <div class="ui-layout-west" style="float:left; width:20%;">
        <div class="west-Panel">
            <div class="panel-Title" style="padding:0px; text-align:center;">项目类型</div>
            <div id="itemTree1">
            </div>

        </div>
    </div>
    <div style="float:left;border:solid 1px #ccc; height:700px;"></div>
    <div class="ui-layout-center" style="float:left; width:75%;">
        <div style="height:10px;"></div>
        <div>
            <table class="form" id="WIform">
                <tr>
                    <td class="formTitle">项目名称<font face="宋体">*</font></td>
                    <td class="formValue" colspan="3"><input id="Name" type="text" class="form-control" isvalid="yes" checkexpession="NotNull" /></td>
                    <td class="formTitle">项目编号<font face="宋体">*</font></td>
                    <td class="formValue" colspan="3"><input id="Code" type="text" class="form-control" isvalid="yes" checkexpession="NotNull" /></td>
                </tr>
                <tr>
                    <td class="formTitle">项目类型<font face="宋体">*</font></td>
                    <td class="formValue" colspan="3"><input id="Type" type="hidden" value="" /><input id="TypeText" type="text" placeholder="点击左侧树选取" readonly class="form-control" isvalid="yes" checkexpession="NotNull" /></td>
                    <td class="formTitle">所属机构</td>
                    <td class="formValue" colspan="3">
                        <div id="OrganizeId" type="selectTree"  class="ui-select"></div>
                    </td>
                </tr>
                <tr>
                    <td class="formTitle">频次要求</td>
                    <td class="formValue" colspan="3">
                        <div class="checkbox" style="margin-left:-15px;">
                            <label>
                                <input id="UnitNum" type="number" isvalid="yes" checkexpession="NotNull" value="1" readonly="readonly" min="1" style="width:40px;" />
                                <select id="Unit" style="border: 0px;">
                                    <option value="0">天</option>
                                    <option value="1">周</option>
                                    <option selected="selected" value="2">月</option>
                                    <option value="3">季</option>
                                    <option value="4">年</option>
                                </select>
                                <input type="number" style="width:40px;" id="Frequency" min="1" isvalid="yes" checkexpession="NotNull" />&nbsp;次
                            </label>
                        </div>
                    </td>

                    <td class="formTitle">执行岗位</td>
                    <td class="formValue" colspan="3">
                        <div id="ExecRoleId" type="select" class="ui-select"></div>
                    </td>

                </tr>

                <tr>
                    <td class="formTitle" valign="top" style="padding-top: 4px;">工作内容说明</td>
                    <td class="formValue" colspan="7">
                        <textarea id="Memo" class="form-control" style="height: 50px;"></textarea>
                    </td>
                </tr>
                <tr>
                    <th class="formTitle">应用设备等级</th>
                    <td class="formValue" colspan="7">
                        <div id="Level" class="checkbox user-select">
                        </div>
                    </td>
                </tr>
                <tr>
                    <th class="formTitle">应用项目类型</th>
                    <td class="formValue" colspan="7">
                        <div id="ProjectType" class="checkbox user-select">
                        </div>
                    </td>
                </tr>
                <tr>
                    <th class="formTitle" valign="top" style="padding-top: 4px;">应用设备类别</th>
                    <td class="formValue" colspan="7">
                        <div id="DeviceType" class="form-control"></div>
                    </td>
                </tr>
            </table>
        </div>

        <div>
            <ul class="nav nav-tabs" style="background-color:#f6f8f8;">
                <li class="active" onclick="BaseClick(0)">
                    <a href="#panel1" data-toggle="tab">作业要点</a>
                </li>
                <li onclick="BaseClick(1)">
                    <a href="#panel2" data-toggle="tab">关联设备</a>
                </li>

            </ul>
        </div>
        <div class="tab-content">

            <div class="tab-pane active" id="panel1">
                <div class="titlePanel">
                    <div style="margin: 5px;  margin-top: 0px; margin-bottom: 0px;">
                        <table id="gridTable"></table>
                        <style>
                            .ui-jqgrid .ui-jqgrid-caption {
                                text-align: right;
                                padding-right: 5px;
                                background-color: #F5F5F5;
                            }
                        </style>
                    </div>
                    <div class="toolbar" style=" position: absolute; top: -15px; left: 20px;">
                        <a id="lr-addField" class="btn btn-success btn-xs" onclick="btn_add()"><i class="fa fa-plus"></i>&nbsp;添加</a>
                        <a id="lr-editField" class="btn btn-info btn-xs" onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>&nbsp;修改</a>
                        <a id="lr-deleteField" class="btn btn-danger btn-xs" onclick="btn_delete()"><i class="fa fa-trash-o"></i>&nbsp;移除</a>

                    </div>
                </div>
            </div>
            <div class="tab-pane" id="panel2">
                <div class="titlePanel">
                    <table id="gridTable1"></table>
                    <div id="gridPager"></div>

                </div>
            </div>
        </div>
    </div>
</div>
